package com.ocom.emqx.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.AcpService;
import com.ocom.common.entity.company.EntityCompanyConfig;
import com.ocom.common.enums.OpTypeDiffEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payhandle.QrCardPayRequest;
import com.ocom.common.request.payhandle.QrCodePayRequest;
import com.ocom.common.resp.RespMachMessage;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.*;
import com.ocom.common.vo.devicehttp.MachMealDataVo;
import com.ocom.emqx.client.MachineFeignService;
import com.ocom.emqx.client.PayoperFeignService;
import com.ocom.emqx.handle.MqttSendClient;
import com.ocom.emqx.service.HnQrCodeMachineService;
import com.ocom.emqx.timer.UserRunnableTask;
import com.ocom.emqx.vo.QrCodeMachineVo;
import com.ocom.emqx.vo.QrCodePayingVo;
import com.ocom.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ocom.emqx.vo.HnQrCodePayVo;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Service
public class HnQrCodeMachineServiceImpl implements HnQrCodeMachineService {

    @Autowired
    private MachineFeignService machineFeignService;

    @Autowired
    private PayoperFeignService payoperFeignService;

    @Autowired
    MqttSendClient mqttSendClient;

    @Resource
    private RedisUtil redisUtil;

    public Logger log = LoggerFactory.getLogger(HnQrCodeMachineServiceImpl.class);

    @Override
    public void qrCodePay(HnQrCodePayVo hnQrCodePayVo) {

        String orderNo = hnQrCodePayVo.getSn() + hnQrCodePayVo.getMsgid();
        boolean lock1 = redisUtil.lock("mqtt:msgLock:" + orderNo, 100);
        if (!lock1) {
            return;
        }
        try {
            QrCodeMachineVo qrCodeMachineVo = sign(hnQrCodePayVo.getSn());
            if (qrCodeMachineVo == null || qrCodeMachineVo.getCode() != 200) {
                log.info("签到-签到异常- 设备号【{}】请求【{}】异常【{}】", hnQrCodePayVo.getSn(), hnQrCodePayVo, qrCodeMachineVo);
                mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"0\",\"msg\":\"设备未注册\"}");
                return;
            }

//            mqttSendClient.publish(false,hnQrCodePayVo.getSn()+"/submsg","{\"cmd\":\"voice\",\"money\":\"0\",\"msg\":\"继续支付\"}");

            QrCodePayRequest qrCodePayRequest = new QrCodePayRequest();
            try {
                qrCodePayRequest.setMachNo(qrCodeMachineVo.getMachNo());
                qrCodePayRequest.setCompanyId(qrCodeMachineVo.getCompanyId().longValue());
                qrCodePayRequest.setConsType(2);
                qrCodePayRequest.setTradeNo(orderNo);

                qrCodePayRequest.setAmount(Integer.valueOf(AcpService.pointsAmount(hnQrCodePayVo.getMoney())));

                byte[] encrypted1 = Base64.decode(hnQrCodePayVo.getData());
                String tempStr = new String(encrypted1, "utf-8");
                qrCodePayRequest.setAuthCode(tempStr);

                log.info("解码 【{}】【{}】【{}】", hnQrCodePayVo.getSn(), hnQrCodePayVo, tempStr);

                Result result = payoperFeignService.qrCodePay(qrCodePayRequest);

                log.info("请求方式:【{}】,机器号:【{}】,原始参数:【{}】,请求参数:【{}】,返回:【{}】", "支付请求（强在线）mqtt", hnQrCodePayVo.getSn(), hnQrCodePayVo, qrCodePayRequest, result);

                if (result.getCode() == 200) {
                    boolean lock2 = redisUtil.lock("mqtt:orderSucc:" + orderNo, 100);
                    if (!lock2) {
                        return;
                    }
                    Map<String, Object> map = JSON.parseObject(JSON.toJSONString(result.getData()), Map.class);
                    String mes = "收款";

                    if (map.get("opTypeDiff") != null) {
                        Integer type = (Integer) map.get("opTypeDiff");
                        String payName = OpTypeDiffEnum.getName(type);
                        if (payName != null) mes = payName + mes;
                    }

                    mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"" + mes + hnQrCodePayVo.getMoney() + "元" + "\"}");
                } else {
                    if (result.getCode() == 3030) {
                        boolean lock2 = redisUtil.lock("mqtt:orderSucc:" + orderNo, 100);
                        if (!lock2) {
                            return;
                        }
                        log.info("请求方式:【{}】,机器号:【{}】【{}】,,请求参数:【{}】,返回失败:【{}】", "支付请求（强在线）web", hnQrCodePayVo.getSn(), hnQrCodePayVo, qrCodePayRequest, result);
                        mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"" + result.getMsg() + "\"}");
                        return;
                    }
                    //支付中的处理
                    QrCodePayingVo qrCodePayingVo = new QrCodePayingVo();
                    qrCodePayingVo.setCompanyId(qrCodeMachineVo.getCompanyId());
                    qrCodePayingVo.setMachNo(hnQrCodePayVo.getSn());
                    qrCodePayingVo.setTradeNo(orderNo);
                    qrCodePayingVo.setMoney(hnQrCodePayVo.getMoney());
                    redisUtil.zAdd("mqtt:orderPaying", qrCodePayingVo, System.currentTimeMillis());

                    ThreadTaskUtils.run(new UserRunnableTask(qrCodePayingVo, redisUtil, mqttSendClient, payoperFeignService, log));
                    log.info("插入定时查询 【{}】【{}】【{}】", qrCodePayingVo.getMachNo(), qrCodePayingVo.getTradeNo(), qrCodePayingVo);

                }
            } catch (Exception e) {
                log.info("请求方式:【{}】,机器号:【{}】,请求参数:【{}】,异常:【{}】", "支付请求（强在线）web", hnQrCodePayVo.getSn(), qrCodePayRequest, e);
                mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"系统异常\"}");
                return;
            }

        } catch (Exception e) {
            log.info("mqtt扫码异常- 设备号【{}】请求【{}】异常【{}】", hnQrCodePayVo.getSn(), hnQrCodePayVo, e);
            try {
                mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"0\",\"msg\":\"扫码异常\"}");
            } catch (Exception e4) {
            }
        }
    }

    @Override
    public void qrCodePayB(HnQrCodePayVo hnQrCodePayVo) {

        String orderNo = hnQrCodePayVo.getSn() + hnQrCodePayVo.getMsgid();
        boolean lock1 = redisUtil.lock("mqtt:msgLock:" + orderNo, 100);
        if (!lock1) {
            return;
        }
        String messageId = String.format("%s%s", RandomUtil.randomString(20), DateUtil.date().getTime());
        try {
            QrCodeMachineVo qrCodeMachineVo = signB(hnQrCodePayVo.getSn());
            if (qrCodeMachineVo == null || qrCodeMachineVo.getCode() != 200) {
                log.info("签到-签到异常- 设备号【{}】请求【{}】异常【{}】", hnQrCodePayVo.getSn(), hnQrCodePayVo, qrCodeMachineVo);
                mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/subHandheld", "{\"cmd\":\"voice\",\"msg\":\"设备未注册\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msgid\":\"" + messageId + "\"}");
                return;
            }

//            mqttSendClient.publish(false,hnQrCodePayVo.getSn()+"/submsg","{\"cmd\":\"voice\",\"money\":\"0\",\"msg\":\"继续支付\"}");

            QrCodePayRequest qrCodePayRequest = new QrCodePayRequest();
            try {
                qrCodePayRequest.setMachNo(qrCodeMachineVo.getMachNo());
                qrCodePayRequest.setCompanyId(qrCodeMachineVo.getCompanyId().longValue());
                qrCodePayRequest.setConsType(2);
                qrCodePayRequest.setTradeNo(orderNo);

                qrCodePayRequest.setAmount(Integer.valueOf(AcpService.pointsAmount(hnQrCodePayVo.getMoney())));

                byte[] encrypted1 = Base64.decode(hnQrCodePayVo.getData());
                String tempStr = new String(encrypted1, "utf-8");
                qrCodePayRequest.setAuthCode(tempStr);

                log.info("解码 【{}】【{}】【{}】", hnQrCodePayVo.getSn(), hnQrCodePayVo, tempStr);

                Result result = payoperFeignService.qrCodePay(qrCodePayRequest);

                log.info("请求方式:【{}】,机器号:【{}】,原始参数:【{}】,请求参数:【{}】,返回:【{}】", "支付请求（强在线）mqtt", hnQrCodePayVo.getSn(), hnQrCodePayVo, qrCodePayRequest, result);

                if (result.getCode() == 200) {
                    boolean lock2 = redisUtil.lock("mqtt:orderSucc:" + orderNo, 100);
                    if (!lock2) {
                        return;
                    }
                    Map<String, Object> map = JSON.parseObject(JSON.toJSONString(result.getData()), Map.class);
                    String mes = "收款";

                    if (map.get("opTypeDiff") != null) {
                        Integer type = (Integer) map.get("opTypeDiff");
                        String payName = OpTypeDiffEnum.getName(type);
                        if (payName != null) mes = payName + mes;
                    }

                    mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/subHandheld", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"" + mes + hnQrCodePayVo.getMoney() + "元" + "\",\"msgid\":\"" + messageId + "\"}");
                } else {
                    if (result.getCode() == 3030) {
                        boolean lock2 = redisUtil.lock("mqtt:orderSucc:" + orderNo, 100);
                        if (!lock2) {
                            return;
                        }
                        log.info("请求方式:【{}】,机器号:【{}】【{}】,,请求参数:【{}】,返回失败:【{}】", "支付请求（强在线）web", hnQrCodePayVo.getSn(), hnQrCodePayVo, qrCodePayRequest, result);
                        mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/subHandheld", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"" + result.getMsg() + "\",\"msgid\":\"" + messageId + "\"}");
                        return;
                    }
                    //支付中的处理
                    QrCodePayingVo qrCodePayingVo = new QrCodePayingVo();
                    qrCodePayingVo.setCompanyId(qrCodeMachineVo.getCompanyId());
                    qrCodePayingVo.setMachNo(hnQrCodePayVo.getSn());
                    qrCodePayingVo.setTradeNo(orderNo);
                    qrCodePayingVo.setMoney(hnQrCodePayVo.getMoney());
                    redisUtil.zAdd("mqtt:orderPaying", qrCodePayingVo, System.currentTimeMillis());

                    ThreadTaskUtils.run(new UserRunnableTask(qrCodePayingVo, redisUtil, mqttSendClient, payoperFeignService, log));
                    log.info("插入定时查询 【{}】【{}】【{}】", qrCodePayingVo.getMachNo(), qrCodePayingVo.getTradeNo(), qrCodePayingVo);

                }
            } catch (Exception e) {
                log.info("请求方式:【{}】,机器号:【{}】,请求参数:【{}】,异常:【{}】", "支付请求（强在线）web", hnQrCodePayVo.getSn(), qrCodePayRequest, e);
                mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/subHandheld", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"系统异常\",\"msgid\":\"" + messageId + "\"}");
                return;
            }

        } catch (Exception e) {
            log.info("mqtt扫码异常- 设备号【{}】请求【{}】异常【{}】", hnQrCodePayVo.getSn(), hnQrCodePayVo, e);
            try {
                mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/subHandheld", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"扫码异常\",\"msgid\":\"" + messageId + "\"}");
            } catch (Exception e4) {
            }
        }
    }


    public QrCodeMachineVo sign(String machno) {
        String canKey = "MqttXfDevice:" + machno;
        Object requestRedis = redisUtil.get(canKey);
        if (requestRedis != null) {
            if (requestRedis.toString().equals("null")) {
                return null;
            }
            return (QrCodeMachineVo) requestRedis;
        }

        redisUtil.set("MqttXfDevice:" + machno, "null", 30l);

        QrCodeMachineVo qrCodeMachineVo = new QrCodeMachineVo();
        qrCodeMachineVo.setMachNo(machno);
        try {
            //机器号
            RespMachMessage respMachMessage = machineFeignService.getDeviceState(machno);
            if (respMachMessage.getCode().equals("0000")) {
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(respMachMessage.getData()));
                //获取单位编号
                Integer companyId = jsonObject.getInteger("companyId");
                if (companyId == null) {
                    qrCodeMachineVo.setCode(1);
                    qrCodeMachineVo.setMsg(StateCode.companyIdMsg);
                    return qrCodeMachineVo;
                }
                //SaaS平台编号
                Integer saasNo = jsonObject.getInteger("saasNo");

                //平台编号为1按照本平台走
                if (saasNo == 1) {
                    try {
                        Result result = payoperFeignService.GetMerchInfoByMachNo(companyId.longValue(), machno);
                        log.info("签到-获取商户名称- 【{}】【{}】", machno, result);
                        if (result.getCode() == 200) {
                            /**
                             * RespMachMessage{code='200', msg='成功', data={status=0, machNo=24050901, deviceName=测试-zhuhui, comId=52, companyName=公司测试, pointId=358, pointName=测试离线补缴, merchId=309, merchName=测试补缴商户}}
                             */
                            Map<String, Object> map = JSON.parseObject(JSON.toJSONString(result.getData()), Map.class);

                            String mes = "当前绑定商户为";
                            Integer merchId = (Integer) map.get("merchId");
                            if (merchId == 0) {
                                mes += "本单位";
                                qrCodeMachineVo.setMerchName("本单位");
                            } else {
                                qrCodeMachineVo.setMerchName((String) map.get("merchName"));
                                mes += qrCodeMachineVo.getMerchName();
                                Integer pointId = (Integer) map.get("pointId");
                                qrCodeMachineVo.setPointId(0);
                                if (pointId >= 0) {
                                    mes += ",消费点名称为" + map.get("pointName");
                                }
                            }
                            mqttSendClient.publish(false, machno + "/submsg", "{\"cmd\": \"setpara\",\"poweron\": \"" + mes + ",欢迎使用云卡通3.0收款音箱\"}");

                            qrCodeMachineVo.setMerchId(merchId);
                            qrCodeMachineVo.setCompanyId(companyId);
                            qrCodeMachineVo.setSaasNo(saasNo);
                            redisUtil.set("MqttXfDevice:" + machno, qrCodeMachineVo, 86400l);
                            return qrCodeMachineVo;
                        }
                        qrCodeMachineVo.setCode(result.getCode());
                        qrCodeMachineVo.setMsg(result.getMsg());
                        return qrCodeMachineVo;
                    } catch (Exception e) {
                        log.info("机器号:【{}】,请求方式:【{}】,返回参数:【{}】", machno, "mqtt消费签到2", e);
                        qrCodeMachineVo.setCode(3);
                        qrCodeMachineVo.setMsg("系统异常，请联系管理员2");
                        return qrCodeMachineVo;
                    }


                } else {
                    qrCodeMachineVo.setCode(Integer.valueOf(StateCode.saasNoCod));
                    qrCodeMachineVo.setMsg(StateCode.saasNoMsg);
                    return qrCodeMachineVo;
                }
            } else {
                qrCodeMachineVo.setCode(Integer.valueOf(respMachMessage.getCode()));
                qrCodeMachineVo.setMsg(respMachMessage.getMsg());
                return qrCodeMachineVo;
            }
        } catch (Exception e) {
            log.info("机器号:【{}】,请求方式:【{}】,返回参数:【{}】", machno, "mqtt消费签到1", e);
            qrCodeMachineVo.setCode(3);
            qrCodeMachineVo.setMsg("系统异常，请联系管理员1");
            return qrCodeMachineVo;
        }
    }

    public QrCodeMachineVo signB(String machno) {
        String canKey = "MqttXfDevice:" + machno;
        Object requestRedis = redisUtil.get(canKey);
        String messageId = String.format("%s%s", RandomUtil.randomString(20), DateUtil.date().getTime());
        if (requestRedis != null) {
            if (requestRedis.toString().equals("null")) {
                return null;
            }
            return (QrCodeMachineVo) requestRedis;
        }

        redisUtil.set("MqttXfDevice:" + machno, "null", 30l);

        QrCodeMachineVo qrCodeMachineVo = new QrCodeMachineVo();
        qrCodeMachineVo.setMachNo(machno);
        try {
            //机器号
            RespMachMessage respMachMessage = machineFeignService.getDeviceState(machno);
            if (respMachMessage.getCode().equals("0000")) {
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(respMachMessage.getData()));
                //获取单位编号
                Integer companyId = jsonObject.getInteger("companyId");
                if (companyId == null) {
                    qrCodeMachineVo.setCode(1);
                    qrCodeMachineVo.setMsg(StateCode.companyIdMsg);
                    return qrCodeMachineVo;
                }
                //SaaS平台编号
                Integer saasNo = jsonObject.getInteger("saasNo");

                //平台编号为1按照本平台走
                if (saasNo == 1) {
                    try {
                        Result result = payoperFeignService.GetMerchInfoByMachNo(companyId.longValue(), machno);
                        log.info("签到-获取商户名称- 【{}】【{}】", machno, result);
                        if (result.getCode() == 200) {
                            /**
                             * RespMachMessage{code='200', msg='成功', data={status=0, machNo=24050901, deviceName=测试-zhuhui, comId=52, companyName=公司测试, pointId=358, pointName=测试离线补缴, merchId=309, merchName=测试补缴商户}}
                             */
                            Map<String, Object> map = JSON.parseObject(JSON.toJSONString(result.getData()), Map.class);

                            String mes = "当前绑定商户为";
                            Integer merchId = (Integer) map.get("merchId");
                            if (merchId == 0) {
                                mes += "本单位";
                                qrCodeMachineVo.setMerchName("本单位");
                            } else {
                                qrCodeMachineVo.setMerchName((String) map.get("merchName"));
                                mes += qrCodeMachineVo.getMerchName();
                                Integer pointId = (Integer) map.get("pointId");
                                qrCodeMachineVo.setPointId(0);
                                if (pointId >= 0) {
                                    mes += ",消费点名称为" + map.get("pointName");
                                }
                            }
//                            mqttSendClient.publish(false, machno + "/submsg", "{\"cmd\": \"setpara\",\"poweron\": \""+mes+",欢迎使用云卡通3.0收款音箱\"}");
                            mqttSendClient.publish(false, machno + "/subHandheld", "{\"cmd\":\"voice\",\"msg\":\"" + mes + "欢迎使用云卡通3.0收款音箱\",\"msgid\":\"" + messageId + "\"}");
                            qrCodeMachineVo.setMerchId(merchId);
                            qrCodeMachineVo.setCompanyId(companyId);
                            qrCodeMachineVo.setSaasNo(saasNo);
                            redisUtil.set("MqttXfDevice:" + machno, qrCodeMachineVo, 86400l);
                            return qrCodeMachineVo;
                        }
                        qrCodeMachineVo.setCode(result.getCode());
                        qrCodeMachineVo.setMsg(result.getMsg());
                        return qrCodeMachineVo;
                    } catch (Exception e) {
                        log.info("机器号:【{}】,请求方式:【{}】,返回参数:【{}】", machno, "mqtt消费签到2", e);
                        qrCodeMachineVo.setCode(3);
                        qrCodeMachineVo.setMsg("系统异常，请联系管理员2");
                        return qrCodeMachineVo;
                    }


                } else {
                    qrCodeMachineVo.setCode(Integer.valueOf(StateCode.saasNoCod));
                    qrCodeMachineVo.setMsg(StateCode.saasNoMsg);
                    return qrCodeMachineVo;
                }
            } else {
                qrCodeMachineVo.setCode(Integer.valueOf(respMachMessage.getCode()));
                qrCodeMachineVo.setMsg(respMachMessage.getMsg());
                return qrCodeMachineVo;
            }
        } catch (Exception e) {
            log.info("机器号:【{}】,请求方式:【{}】,返回参数:【{}】", machno, "mqtt消费签到1", e);
            qrCodeMachineVo.setCode(3);
            qrCodeMachineVo.setMsg("系统异常，请联系管理员1");
            return qrCodeMachineVo;
        }
    }

    @Override
    public void qrPayCard(HnQrCodePayVo hnQrCodePayVo) {
        String orderNo = hnQrCodePayVo.getSn() + hnQrCodePayVo.getMsgid();
        boolean lock1 = redisUtil.lock("mqtt:msgLock:" + orderNo, 100);
        if (!lock1) {
            return;
        }
        try {
            QrCodeMachineVo qrCodeMachineVo = sign(hnQrCodePayVo.getSn());
            if (qrCodeMachineVo == null || qrCodeMachineVo.getCode() != 200) {
                log.info("签到-签到异常- 设备号【{}】请求【{}】异常【{}】", hnQrCodePayVo.getSn(), hnQrCodePayVo, qrCodeMachineVo);
                mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"0\",\"msg\":\"设备未注册\"}");
                return;
            }
            QrCardPayRequest qrCardPayRequest = new QrCardPayRequest();
            try {
                qrCardPayRequest.setMachNo(qrCodeMachineVo.getMachNo());
                qrCardPayRequest.setCompanyId(qrCodeMachineVo.getCompanyId().longValue());
                qrCardPayRequest.setConsType(1);
                qrCardPayRequest.setTradeNo(orderNo);
                qrCardPayRequest.setType(21);

                qrCardPayRequest.setAmount(Integer.valueOf(AcpService.pointsAmount(hnQrCodePayVo.getMoney())));

                byte[] encrypted1 = Base64.decode(hnQrCodePayVo.getData());
                String tempStr = new String(encrypted1, "utf-8");
                qrCardPayRequest.setAuthCode(tempStr);

                log.info("解码 【{}】【{}】【{}】", hnQrCodePayVo.getSn(), hnQrCodePayVo, tempStr);

                Result result = payoperFeignService.getPayCard(qrCardPayRequest);

                log.info("请求方式:【{}】,机器号:【{}】,原始参数:【{}】,请求参数:【{}】,返回:【{}】", "支付请求（强在线）mqtt", hnQrCodePayVo.getSn(), hnQrCodePayVo, qrCardPayRequest, result);

                if (result.getCode() == 200) {
                    boolean lock2 = redisUtil.lock("mqtt:orderSucc:" + orderNo, 100);
                    if (!lock2) {
                        return;
                    }
                    Map<String, Object> map = JSON.parseObject(JSON.toJSONString(result.getData()), Map.class);
                    String mes = "收款";

                    if (map.get("opTypeDiff") != null) {
                        Integer type = (Integer) map.get("opTypeDiff");
                        String payName = OpTypeDiffEnum.getName(type);
                        if (payName != null) mes = payName + mes;
                    }

                    mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"" + mes + hnQrCodePayVo.getMoney() + "元" + "\"}");
                } else {
                    if (result.getCode() == 3030) {
                        boolean lock2 = redisUtil.lock("mqtt:orderSucc:" + orderNo, 100);
                        if (!lock2) {
                            return;
                        }
                        log.info("请求方式:【{}】,机器号:【{}】【{}】,,请求参数:【{}】,返回失败:【{}】", "支付请求（强在线）web", hnQrCodePayVo.getSn(), hnQrCodePayVo, qrCardPayRequest, result);
                        mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"" + result.getMsg() + "\"}");
                        return;
                    }
                    //支付中的处理
                    QrCodePayingVo qrCodePayingVo = new QrCodePayingVo();
                    qrCodePayingVo.setCompanyId(qrCodeMachineVo.getCompanyId());
                    qrCodePayingVo.setMachNo(hnQrCodePayVo.getSn());
                    qrCodePayingVo.setTradeNo(orderNo);
                    qrCodePayingVo.setMoney(hnQrCodePayVo.getMoney());
                    redisUtil.zAdd("mqtt:orderPaying", qrCodePayingVo, System.currentTimeMillis());

                    ThreadTaskUtils.run(new UserRunnableTask(qrCodePayingVo, redisUtil, mqttSendClient, payoperFeignService, log));
                    log.info("插入定时查询 【{}】【{}】【{}】", qrCodePayingVo.getMachNo(), qrCodePayingVo.getTradeNo(), qrCodePayingVo);

                }
            } catch (Exception e) {
                log.info("请求方式:【{}】,机器号:【{}】,请求参数:【{}】,异常:【{}】", "支付请求（强在线）web", hnQrCodePayVo.getSn(), qrCardPayRequest, e);
                mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"系统异常\"}");
                return;
            }

        } catch (Exception e) {
            log.info("mqtt扫码异常- 设备号【{}】请求【{}】异常【{}】", hnQrCodePayVo.getSn(), hnQrCodePayVo, e);
            try {
                mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/submsg", "{\"cmd\":\"voice\",\"money\":\"0\",\"msg\":\"扫码异常\"}");
            } catch (Exception e4) {
            }
        }
    }

    @Override
    public void qrPayCardB(HnQrCodePayVo hnQrCodePayVo) {
        String orderNo = hnQrCodePayVo.getSn() + hnQrCodePayVo.getMsgid();
        boolean lock1 = redisUtil.lock("mqtt:msgLock:" + orderNo, 100);
        if (!lock1) {
            return;
        }
        String messageId = String.format("%s%s", RandomUtil.randomString(20), DateUtil.date().getTime());
        try {
            QrCodeMachineVo qrCodeMachineVo = signB(hnQrCodePayVo.getSn());
            if (qrCodeMachineVo == null || qrCodeMachineVo.getCode() != 200) {
                log.info("签到-签到异常- 设备号【{}】请求【{}】异常【{}】", hnQrCodePayVo.getSn(), hnQrCodePayVo, qrCodeMachineVo);
                mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/subHandheld", "{\"cmd\":\"voice\",\"msg\":\"设备未注册\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msgid\":\"" + messageId + "\"}");
                return;
            }

//            mqttSendClient.publish(false,hnQrCodePayVo.getSn()+"/submsg","{\"cmd\":\"voice\",\"money\":\"0\",\"msg\":\"继续支付\"}");

            QrCardPayRequest qrCardPayRequest = new QrCardPayRequest();
            try {
                qrCardPayRequest.setMachNo(qrCodeMachineVo.getMachNo());
                qrCardPayRequest.setCompanyId(qrCodeMachineVo.getCompanyId().longValue());
                qrCardPayRequest.setConsType(1);
                qrCardPayRequest.setTradeNo(orderNo);
                qrCardPayRequest.setType(21);

                qrCardPayRequest.setAmount(Integer.valueOf(AcpService.pointsAmount(hnQrCodePayVo.getMoney())));

                byte[] encrypted1 = Base64.decode(hnQrCodePayVo.getData());
                String tempStr = new String(encrypted1, "utf-8");
                qrCardPayRequest.setAuthCode(tempStr);

                log.info("解码 【{}】【{}】【{}】", hnQrCodePayVo.getSn(), hnQrCodePayVo, tempStr);

                Result result = payoperFeignService.getPayCard(qrCardPayRequest);

                log.info("请求方式:【{}】,机器号:【{}】,原始参数:【{}】,请求参数:【{}】,返回:【{}】", "支付请求（强在线）mqtt", hnQrCodePayVo.getSn(), hnQrCodePayVo, qrCardPayRequest, result);

                if (result.getCode() == 200) {
                    boolean lock2 = redisUtil.lock("mqtt:orderSucc:" + orderNo, 100);
                    if (!lock2) {
                        return;
                    }
                    Map<String, Object> map = JSON.parseObject(JSON.toJSONString(result.getData()), Map.class);
                    String mes = "收款";

                    if (map.get("opTypeDiff") != null) {
                        Integer type = (Integer) map.get("opTypeDiff");
                        String payName = OpTypeDiffEnum.getName(type);
                        if (payName != null) mes = payName + mes;
                    }

                    mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/subHandheld", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"" + mes + hnQrCodePayVo.getMoney() + "元" + "\",\"msgid\":\"" + messageId + "\"}");
                } else {
                    if (result.getCode() == 3030) {
                        boolean lock2 = redisUtil.lock("mqtt:orderSucc:" + orderNo, 100);
                        if (!lock2) {
                            return;
                        }
                        log.info("请求方式:【{}】,机器号:【{}】【{}】,,请求参数:【{}】,返回失败:【{}】", "支付请求（强在线）web", hnQrCodePayVo.getSn(), hnQrCodePayVo, qrCardPayRequest, result);
                        mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/subHandheld", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"" + result.getMsg() + "\",\"msgid\":\"" + messageId + "\"}");
                        return;
                    }
                    //支付中的处理
                    QrCodePayingVo qrCodePayingVo = new QrCodePayingVo();
                    qrCodePayingVo.setCompanyId(qrCodeMachineVo.getCompanyId());
                    qrCodePayingVo.setMachNo(hnQrCodePayVo.getSn());
                    qrCodePayingVo.setTradeNo(orderNo);
                    qrCodePayingVo.setMoney(hnQrCodePayVo.getMoney());
                    redisUtil.zAdd("mqtt:orderPaying", qrCodePayingVo, System.currentTimeMillis());

                    ThreadTaskUtils.run(new UserRunnableTask(qrCodePayingVo, redisUtil, mqttSendClient, payoperFeignService, log));
                    log.info("插入定时查询 【{}】【{}】【{}】", qrCodePayingVo.getMachNo(), qrCodePayingVo.getTradeNo(), qrCodePayingVo);

                }
            } catch (Exception e) {
                log.info("请求方式:【{}】,机器号:【{}】,请求参数:【{}】,异常:【{}】", "支付请求（强在线）web", hnQrCodePayVo.getSn(), qrCardPayRequest, e);
                mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/subHandheld", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"系统异常\",\"msgid\":\"" + messageId + "\"}");
                return;
            }

        } catch (Exception e) {
            log.info("mqtt扫码异常- 设备号【{}】请求【{}】异常【{}】", hnQrCodePayVo.getSn(), hnQrCodePayVo, e);
            try {
                mqttSendClient.publish(false, hnQrCodePayVo.getSn() + "/subHandheld", "{\"cmd\":\"voice\",\"money\":\"" + hnQrCodePayVo.getMoney() + "\",\"msg\":\"扫码异常\",\"msgid\":\"" + messageId + "\"}");
            } catch (Exception e4) {
            }
        }
    }
}
